/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kz.dasm.baing.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kreedmart
 */
public class JdbcHelper {
  private static final Logger logger = 
          Logger.getLogger(JdbcHelper.class.getName());
  
  public static void safeClose(Connection c) {
    if (c != null) {
      try {
        c.close();
      } 
      catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
      }
    }
  }
  
  
  public static void safeClose(Statement s) {
    if (s != null) {
      try {
        s.close();        
      } 
      catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
      }
    }
  }
  

  public static void safeClose(Connection c, Statement s) {
    safeClose(s);
    safeClose(c);
  }
  
  
  public static String[] getResultSetColumnNames(ResultSet rs) throws SQLException {
    List<String> columns = new ArrayList<String>();
    ResultSetMetaData rsmd = rs.getMetaData();
    
    int columnCount = rsmd.getColumnCount();
    
    for (int i = 1; i <= columnCount; i++) {
      columns.add(rsmd.getColumnName(i));
    }            

    return columns.toArray(new String[0]); 
  }
}
